package com.controll;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pojo.Archive;
import com.service.ArchiveService;
import com.service.User_logService;

@Controller
public class ArchiveControll {
	/**
	 * 调用service层方法
	 */
	ArchiveService service = new ArchiveService();
	User_logService service_log = new User_logService();

	@RequestMapping(value = "/add_archive.action", method = RequestMethod.POST)
	@ResponseBody
	/**
	 * 添加档案信息
	 */
	public String add_archive(
			@CookieValue(value = "User", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "archive_id", defaultValue = "") String archive_id,
			@RequestParam(value = "identity", defaultValue = "") String identity,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority) {
		service.add_archive(archive_id, identity, name, gender, birthday,
				date_retire1, date_retire2, sp, fc, title, seniority);
		service_log.add_log("0", userId, "添加信息");
		return "OK";

	}

	@RequestMapping(value = "/query_one.action", method = RequestMethod.POST)
	@ResponseBody
	/**
	 * 通过身份证、档案号查询档案信息
	 */
	public String query_one(
			@CookieValue(value = "User", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "search", defaultValue = "") String search) {

		List<Archive> arc = service.query_one(search);

		if (0 == arc.size()) {
			return "ERR1";
		} else if (1 < arc.size()) {
			return "ERR2";
		}
		service_log.add_log(arc.get(0).getId(), userId, "浏览");
		return arc.get(0).getId();

	}

	@RequestMapping(value = "/queryOne.action", method = RequestMethod.GET)
	@ResponseBody
	/**
	 * 根据id查询一条档案信息	
	 */
	public Archive queryOne(
			@RequestParam(value = "id", defaultValue = "") String id) {
		Archive arc = service.queryOne(id);
		// System.out.println(arc);

		return arc;

	}

	@RequestMapping(value = "/update.action", method = RequestMethod.POST)
	@ResponseBody
	/**
	 * 修改刚查询的信息
	 */
	public String update(
			@CookieValue(value = "User", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") String id,
			@RequestParam(value = "identity", defaultValue = "") String identity,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority) {
		service.update(id, identity, name, gender, birthday, date_retire1,
				date_retire2, sp, fc, title, seniority);
		service_log.add_log(id, userId, "修改");
		return "OK";

	}

	@RequestMapping(value = "/query_mh.action", method = RequestMethod.POST)
	@ResponseBody
	/**
	 * 模糊查询档案信息
	 */
	public List<Archive> query_mh(
			@CookieValue(value = "User", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") String id,
			@RequestParam(value = "archive_id", defaultValue = "") String archive_id,
			@RequestParam(value = "identity", defaultValue = "") String identity,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority) {
		// System.out.println(archive_id);
		List<Archive> list = service.query_mh(id, archive_id, identity, name,
				gender, birthday, date_retire1, date_retire2, sp, fc, title,
				seniority);
		// System.out.println(list);
		return list;
	}

	@RequestMapping(value = "/JQ_query_four.action", method = RequestMethod.GET)
	@ResponseBody
	/**
	 * 最近浏览四条档案信息
	 */
	public List<Archive> JQ_query_four(
			@CookieValue(value = "User", defaultValue = "") String userId,
			HttpServletResponse response) {
		List<Archive> list = service.JQ_query_four(userId);
		return list;

	}

	@RequestMapping(value = "/JQ_add_four.action", method = RequestMethod.GET)
	@ResponseBody
	/**
	 * 最近添加档案信息
	 */
	public List<Archive> JQ_add_four(
			@CookieValue(value = "User", defaultValue = "") String userId,
			HttpServletResponse response) {
		List<Archive> list = service.JQ_add_four();
		return list;
	}

	@RequestMapping(value = "/statis.action", method = RequestMethod.POST)
	@ResponseBody
	/**
	 * 通过实间段统计每个用户的上传档案总数
	 */
	public List<Map> statis(
			@RequestParam(value = "date1", defaultValue = "") String date1,
			@RequestParam(value = "date2", defaultValue = "") String date2) {
		// System.out.println(date1);
		List<Map> map = service.statis(date1, date2);
		System.out.println(map);
		return map;

	}
}
